﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Meraz.Core.Model;
using Meraz.Core.Repositories;
using Ninject;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using Meraz.Core.Parsers;
using PetaPoco;
using Meraz.Core.Helpers;

namespace Meraz.Core.Managers
{
    public interface IFacebookBoardParseManager
    {
        void ParseFiles(string directory);
    }
    public class FacebookBoardParseManager : IFacebookBoardParseManager
    {
        #region Properties
        [Inject]
        public Database DB { get; set; }
        [Inject]
        public IFacebookBoardParser FacebookBoardParser { get; set; }
        [Inject]
        public MyWebClient Client { get; set; }

        #endregion
        public void ParseFiles(string directory)
        {
            DirectoryInfo info = new DirectoryInfo(directory);
            Dictionary<long, string> boards = new Dictionary<long, string>();
            foreach (FileInfo file in info.GetFiles("*.htm*", SearchOption.TopDirectoryOnly))
            {
                FacebookTopicBoard board = FacebookBoardParser.ParseBoard(file);
                //cannot use this because of browser detection
                //foreach (FacebookTopic topic in board.Topics)
                //{
                //    try
                //    {
                //        string content = Client.DownloadString(topic.Url);
                //        if (topic.Url.Equals(Client.ResponseUri.AbsoluteUri))
                //            FacebookBoardParser.ParseTopic(content, topic);
                //    }
                //    catch (Exception ex)
                //    {
                //        Debug.WriteLine("ParseTopic failed: " + ex.ToString());
                //    }
                //}
                if (!boards.Keys.Contains(board.ID))
                    boards.Add(board.ID, board.Name);
                SaveTopics(board);
            }

            foreach (long key in boards.Keys)
            {
                SaveBoard(new FacebookTopicBoard() { ID = key, Name = boards[key] });
            }
        }

        public void SaveBoard(FacebookTopicBoard board)
        {
            var a = DB.SingleOrDefault<FacebookTopicBoard>("SELECT * FROM facebookboard WHERE ID=@0", board.ID);
            if (a == null)
            {
                DB.Insert(board);
            }
            else
            {
                DB.Update(board);
            }
        }

        private void SaveTopics(FacebookTopicBoard board)
        {
            foreach (FacebookTopic topic in board.Topics)
            {
                if (topic.Poster == null) topic.Poster = string.Empty;
                var a = DB.SingleOrDefault<FacebookTopic>("SELECT * FROM facebooktopic WHERE ID=@0 AND BoardID=@1", topic.ID, topic.BoardID);
                if (a == null)
                {
                    topic.Status = StatusType.New.ToString();
                    DB.Insert(topic);
                }
                else
                {
                    DB.Update(topic);
                }
                SavePosts(topic);
            }
        }

        private void SavePosts(FacebookTopic topic)
        {
            foreach (FacebookTopicPost post in topic.Posts)
            {
                if (post.Poster == null) post.Poster = string.Empty;
                var p = DB.SingleOrDefault<FacebookTopicPost>("SELECT * FROM facebookpost WHERE BoardID=@0 AND TopicID = @1 AND ID = @2", post.BoardID, post.TopicID, post.ID);
                if (p == null)
                    DB.Insert(post);
                else
                    DB.Update(post);
                SaveLinks(post);
            }
        }

        private void SaveLinks(FacebookTopicPost post)
        {
            DB.Delete<FacebookTopicPostLink>("WHERE BoardID=@0 AND TopicID=@1 AND PostID=@2", post.BoardID, post.TopicID, post.ID);
            foreach (var link in post.Links)
            {
                DB.Insert(link);
            }
        }
    }
}
